package day02;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Author Stringzhua
 * @Date 2024/10/29 14:19
 * description:
 */
public class Test02 {
    public static void main(String[] args) throws InterruptedException {
        AddThreadX addThread = new AddThreadX();
        DecThreadX decThread = new DecThreadX();

        addThread.start();
        decThread.start();

        addThread.join();
        decThread.join();

        System.out.println(AtomicCount.count);
    }
}

class AtomicCount {
//    public static int count = 0;

    //具备原子性的AtomicInteger整型
    public static AtomicInteger count = new AtomicInteger();

}

//递增线程
class AddThreadX extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
//            AtomicCount.count += 1;
            AtomicCount.count.addAndGet(1);//+=1
        }
    }
}

//递减线程
class DecThreadX extends Thread {
    @Override
    public void run() {
        for (int i = 0; i < 1000; i++) {
//            AtomicCount.count -= 1;
            AtomicCount.count.decrementAndGet();//-=1
        }
    }
}
